package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils.NetType;
import com.hundsun.yr.universal.library.widget.GestureLockView;
import com.hundsun.yr.universal.library.widget.GestureLockView.OnCompleteListener;
import com.hundsun.yr.universal.library.widget.GestureLockView.OnResetListener;
import com.hundsun.yr.universal.library.widget.HSToast;
import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.config.Config.SPPath;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;

/**
 * 设置-修改手势密码
 *
 * @author tonglj
 */
public class GestureLockUpdateActivity extends BaseActivity implements
        OnClickListener {

    /**
     * 手势密码控件
     */
    private GestureLockView gesture_view;
    /***
     * 重置
     */
    private View tv_reset, img_close;
    /**
     * 用于保存手势密码结果
     */
    private HSSharedPreferences mConfigSP;

    @Override
    public void onClick(View v) {
        if (isFastDoubleClick()){
            return;
        }

        switch (v.getId()) {
            case R.id.img_gesture_close:
                finish();
                break;
            case R.id.tv_gesture_skip:
                gesture_view.reset();
                break;
            default:
                break;
        }
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_gesturelockupdate;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void initViewsAndEvents() {
        findViews();
        mConfigSP = new HSSharedPreferences(mContext, SPPath.file_configInfo);
        tv_reset.setOnClickListener(this);
        img_close.setOnClickListener(this);
        gesture_view.setOnCompleteListener(new OnCompleteListener() {

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }

            @Override
            public void onComplete(String result) {
                mConfigSP.putShareProf(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.GESTURE, result); // TODO
                // 保存结果
                new HSToast(mContext).showCustomToast("设置成功");
                finish();
            }
        });

        gesture_view.setOnResetListener(new OnResetListener() {

            @Override
            public void callback() {
                tv_reset.setVisibility(View.VISIBLE);
            }
        });
    }

    private void findViews() {
        gesture_view = findView(R.id.gesture_view);
        tv_reset = findView(R.id.tv_gesture_skip);
        img_close = findView(R.id.img_gesture_close);
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @SuppressWarnings("rawtypes")
    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    /**
     * 防止按钮连续点击
     */
    private static long lastClickTime;
    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        if ( time - lastClickTime < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }
}
